<?php
require_once '../params.inc';
require_once '../session.inc';
require_once '../utils.inc';
require_once '../shop.inc';

if ($par->exist('del')) 
{
	$keyFields = new DecodedFieldValues($par->get('keys'));
	$productId = $keyFields->getValueOf('cf_products.id');
	
	Product::del($productId);
	
	header("Location: " . $par->get('target'));
	exit;
}

$action = $par->get('do');

if ('groups' == $action)
{
	$productId = $par->get('productId');
	
	$groupIds = $par->exist('groups') ? $par->get('groups') : array();
		
	$q = null;
	$q = createQuery("
		DELETE FROM cf_products_in_groups
		WHERE product_id=$productId
	");
	$q->execute();
	$q->close();
	
	$q = createQuery("
		INSERT INTO cf_products_in_groups
			(group_id, product_id)
		VALUES (:groupId, $productId)
	");

	foreach ($groupIds as $groupId) 
	{
		$q->setParam('groupId',$groupId);
		$q->execute();
	}
	
	header("Location: edit_product_attrs.php?productId=$productId");
	exit;
} 
elseif ('attributes' == $action) 
{
	$productId = $par->get('productId');
	
	$attributes = new ProductAttributes($productId, true);
	foreach ($attributes as $attr) 
	{
		$parName = 'attribute_' . $attr->id();
		if ($par->exist($parName)) 
		{
			$attr->setValue($par->get($parName));
		}
	}
	
	$product = new Product($productId);
	$product->setAttributes($attributes);
	
	session::open();
	$target = session::get('cf_orig_target');
	
	header("Location: $target");
	exit;
}
?>